import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/all_drill_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/all_plan.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/all_resource.dart';

class EmergencyHomeController extends GetxController {

  final resChartData = <ChartData>[
    ChartData('应急专家', 0, Colors.blue),
    ChartData('应急队伍', 0, Colors.blue),
    ChartData('周边物质', 0, Colors.blue),
    ChartData('敏感目标', 0, Colors.blue),
  ];

  // 应急预案类别数量统计
  AllPlanEntity allPlanEntity = AllPlanEntity(
    comprehensiveNum: 0,
    majorNum: 0,
    sceneLiveNum: 0,
    specialNum: 0,
  );


  // 应急资源
  EmergencyResource emergencyResource = EmergencyResource(
    expertNum: 0,
    peripherySourcePointNum: 0,
    sourcePointNum: 0,
    teamNum: 0,
  );

  // 应急演练情况
  AllDrillEntity allDrillEntity = AllDrillEntity();

  // init数据
  initData() {
    emergencyPlanCount();
    emergencyDrillCount();
    emergencyResourceCount();
  }

  // 应急预案类别数量统计接口
  void emergencyPlanCount() async {
    final response = await HttpUtil().generateGetRequest(Interface.getEmergencyPlanTypeCount).execute();

    response.onSuccess((result) {
      if(result.data != null) {
        allPlanEntity = AllPlanEntity.fromJson(result.data as Map<String, dynamic>);
        update();
      }
    });
  }


  // 应急演练情况
  void emergencyDrillCount() async {
    final response = await HttpUtil().generateGetRequest<AllDrillEntity>(Interface.getExercisePlanCount).execute();

    response.onSuccess((result) {
      if(result.data != null) {
        allDrillEntity = result.data!;
        update();
      }
    });
  }

  // 获取应急资源统计
  void emergencyResourceCount() async {
    final response = await HttpUtil().generateGetRequest(Interface.getEmergencyResourceCount).execute();

    response.onSuccess((result) {
      if(result.data != null) {
        emergencyResource = EmergencyResource.fromJson(result.data as Map<String, dynamic>);
        for(int i = 0; i < resChartData.length; i++) {
          switch(i) {
            case 0:
              resChartData[i].y = emergencyResource.expertNum;
              break;
            case 1:
              resChartData[i].y = emergencyResource.teamNum;
              break;
            case 2:
              resChartData[i].y = emergencyResource.peripherySourcePointNum;
              break;
            case 3:
              resChartData[i].y = emergencyResource.sourcePointNum;
              break;
          }
        }
        update();
      }
    });
  }

}
